package com.teatime.client.view;

import java.lang.reflect.Constructor;

import com.teatime.client.R;
import com.teatime.client.TLog;
import com.teatime.client.TeaTimeUtils;
import com.teatime.client.Constants;
import com.teatime.client.activity.TActivity;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

public class FormRow extends TableRow implements OnClickListener {
	
	protected int       mLayoutId;
	protected ViewGroup mMainLayout;
	protected LayoutInflater mLayoutFactory;
	protected int       mIconId;
	protected ImageView mImgIcon;
	protected TextView  mText;
	protected String    mHint;
	protected String    mPopupEditorClass;
	protected String    mStartActivityClass;
	protected PopupEditor mPopupEditor;
	
	public FormRow(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}

	protected int dip2px(int dp) {
		return TeaTimeUtils.dip2px(getContext(), dp);
	}

	private void init(AttributeSet attrs) {
		mLayoutId = R.layout.view_editor_row;
		mIconId = -1;
		initAttrs(attrs);
		initViews();
	}
	
	protected void initAttrs(AttributeSet attrs) {
		TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.teatime);
		if (a == null) {
			return;
		}
		
		for (int i = 0; i < a.getIndexCount(); i ++) {
            int attr = a.getIndex(i);
            switch (attr) {
            case R.styleable.teatime_layout:
            	mLayoutId = a.getResourceId(attr, mLayoutId);
            	break;
            case R.styleable.teatime_icon:
            	mIconId = a.getResourceId(attr, mIconId);
            	break;
            case R.styleable.teatime_hint:
            	mHint = a.getString(attr);
            	break;
            case R.styleable.teatime_startActivity:
            	mStartActivityClass = a.getString(attr);
            	break;
            case R.styleable.teatime_popupEditor:
            	mPopupEditorClass = a.getString(attr);
            	break;
            }
        }
        a.recycle();
	}
	
	protected LayoutInflater getLayoutFactory() {
		if (mLayoutFactory == null) {
			mLayoutFactory = LayoutInflater.from(getContext());
		}
		return mLayoutFactory;
		
	}
	
	protected View createViewById(int viewId) {
		if (viewId <= 0) {
			return null;
		}
		LayoutInflater factory = getLayoutFactory();
		return factory.inflate(viewId, null);
	}
	
	private String getFullClassName(String className) {
		if (className.startsWith(".")) {
			return Constants.packageName + className;
		}
		return className;
	}
	
	protected void initViews() {
		mMainLayout = (ViewGroup)createViewById(mLayoutId);
		if (mMainLayout != null) {
			LayoutParams lp = (LayoutParams)generateDefaultLayoutParams();
			lp.weight = 1.0f;
			addView(mMainLayout, lp);
			mImgIcon = (ImageView)mMainLayout.findViewById(R.id.img_edtor_row_icon);
			if (mImgIcon != null) {
				if (mIconId <= 0) {
					mImgIcon.setVisibility(GONE);
				}
				else {
					mImgIcon.setImageResource(mIconId);
					mImgIcon.setVisibility(VISIBLE);
				}
			}
			mText = (TextView)mMainLayout.findViewById(R.id.txt_edtor_row_text);
			if (mText != null) {
				if (mHint != null) {
					mText.setHint(mHint);
				}
			}
		}
		
		try {
			if (mPopupEditorClass != null) {

				Class<?> c = Class.forName(getFullClassName(mPopupEditorClass));
				Constructor<?> con = c.getConstructor(Context.class);
				mPopupEditor = (PopupEditor)con.newInstance(getContext());
				mPopupEditor.setParentRow(this);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			mPopupEditor = null;
			TLog.e("can not instance the PopupEditor - " + mPopupEditorClass 
				+ ", " + e.getMessage());
		}
		
		setOnClickListener(this);
	}
	
	public void setText(String text) {
		if (mText != null) {
			mText.setText(text);
		}
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == this) {
			if (mPopupEditor != null) {
				mPopupEditor.show();
			}
			else if (mStartActivityClass != null) {
				((TActivity)getContext()).startActivity(mStartActivityClass);
			}
		}
	}
	
	public PopupEditor getPopupEditor() {
		return mPopupEditor;
	}

}
